﻿using KuiHuaBaoDian.Services.Battle.Data;
using KuiHuaBaoDian.Services.Battle.Entity;
using UnityEngine;

namespace KuiHuaBaoDian.Services.Battle.Behaviour {

    /// <summary>
    /// 攻击行为
    /// </summary>
    public interface IBattleBehaviour_Attack : IBattleBehaviour {

        /// <summary>
        /// 实体
        /// </summary>
        new public interface IEntity : IBattleBehaviour.IEntity {

            /// <summary>
            /// CD总量
            /// </summary>
            IBattleData CoolDownBase { get; }

            /// <summary>
            /// CD经过的时间
            /// </summary>
            IBattleData CoolDownDuration { get; }
        }

        public class Parameter {

            /// <summary>
            /// 关注对象位置提供者
            /// </summary>
            public IBattleLocalPositionProvider LookAtLocalPositionProvider;
        }

        public class Info : KHBD_BattleBehaviourInfo<Parameter> {
        }
    }

    [Implement(typeof(IBattleBehaviour_Attack))]
    internal sealed class KHBD_BattleBehaviour_Attack : KHBD_BattleBehaviour<IBattleBehaviour_Attack.Info, IBattleBehaviour_Attack.Parameter>, IBattleBehaviour_Attack {

        new private IBattleBehaviour_Attack.IEntity Entity => (IBattleBehaviour_Attack.IEntity)base.Entity;

        private IBattleLocalPositionProvider m_LookAtLocalPositionProvider;

        protected override void Initialize() {
        }

        protected override void Dispose() {
            m_LookAtLocalPositionProvider = null;
            base.Dispose();
        }

        protected override IBattleBehaviour_Attack.Info LogicFrame(IBattleBehaviour_Attack.Parameter parameter) {
            Entity.CoolDownDuration.Value -= Entity.CoolDownBase.Value;

            return new IBattleBehaviour_Attack.Info {
                Parameter = parameter,
                Type = BattleBehaviourType.Attack,
            };
        }

        protected override void Execute(IBattleBehaviour_Attack.Info info) {
            m_LookAtLocalPositionProvider = info.Parameter.LookAtLocalPositionProvider;
        }

        protected override void Update(IBattleBehaviour_Attack.Info info, float interpolation) {
            if (m_LookAtLocalPositionProvider == null) {
                return;
            }
            StateMachine.State = BattleEntityState.Attack;
            var targetLocalPosition = m_LookAtLocalPositionProvider.LocalPosition;
            if (GameObject.transform.localPosition.x < targetLocalPosition.x) {
                GameObject.transform.localScale = new Vector3(-1f, 1f, 1f);
            } else {
                GameObject.transform.localScale = new Vector3(1f, 1f, 1f);
            }
            m_LookAtLocalPositionProvider = null;
        }
    }
}
